package myky.com.doctorapp.ui


import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.util.Log
import android.view.View
import com.gyf.barlibrary.ImmersionBar
import io.reactivex.annotations.Nullable
import kotlinx.android.synthetic.main.activity_health_record.*


import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.HealthRecordAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.HealthRecordActivityContract
import myky.com.doctorapp.entity.HealthItemEntity
import myky.com.doctorapp.entity.HealthRecordEntity
import myky.com.doctorapp.presenter.HealthRecordActivityPresenter
import myky.com.doctorapp.utils.Constants

/**
 * Description: 健康档案快照
 * @author wuyang
 * Created by 2019/3/13 17:54
 */
class HealthRecordActivity : BaseActivity<HealthRecordActivityPresenter,HealthRecordEntity>(),HealthRecordActivityContract.View {

    lateinit var healthAdapter:HealthRecordAdapter
    lateinit var healthList:MutableList<HealthItemEntity>

    override fun getPresenter(): HealthRecordActivityPresenter = HealthRecordActivityPresenter()
    override fun reloadActivity() {

    }

    override fun injectTarget(): View =findViewById(R.id.health_record_lin)

    override fun initData() {
        var bundle = intent.extras
        if(bundle!=null) {
            var serializable = bundle.getSerializable(Constants.HEALTHELIST)
            showData(serializable as HealthRecordEntity.PerProfileBean)
        }else{
            mPresenter.pfindHealthRecord()

        }

    }

    override fun initView() {

        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.title_health_lin)
                .init()
        conmonTitleTextViews.text=getString(R.string.RemoteSubmit_health)

        healthList=ArrayList()
        healthAdapter= HealthRecordAdapter(healthList)
        health_record_recycler.layoutManager=LinearLayoutManager(this)
        health_record_recycler.adapter=healthAdapter
        backLayout.setOnClickListener { finish() }
    }

    override fun getContenView(): Int =R.layout.activity_health_record
    override fun showDataSuccess(datas: HealthRecordEntity) {
        showData(datas.perProfile)
    }

    private fun showData(perProfile: HealthRecordEntity.PerProfileBean) {



        if (perProfile!=null) {

            var stringArray = resources.getStringArray(R.array.health_record)



            for (i in 0 until stringArray.size)  {


                var healthItemEntity: HealthItemEntity = HealthItemEntity()

                healthItemEntity.title = stringArray[i]

                when (i) {
                    0 -> {
                        healthItemEntity.type=HealthItemEntity.DEFAULT
                    }
                    1 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        healthItemEntity.content = perProfile.name
                    }
                    2 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        healthItemEntity.content = perProfile.birthday.substring(0,10)
                    }
                    3 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        if(perProfile.sex!=-1) {
                            val s = resources.getStringArray(R.array.sex_list)[perProfile.sex]
                            healthItemEntity.content = s
                        }
                    }
                    4 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        healthItemEntity.content = perProfile.bloodType
                    }
                    5 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        healthItemEntity.content = perProfile.idCardNumber
                    }
                    6 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        if (perProfile.maritalStatusId!=-1) {
                            healthItemEntity.content = resources.getStringArray(R.array.marital_status)[perProfile.maritalStatusId]
                        }
                    }
                    7 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        if (perProfile.fertilityNumber!=-1){
                            healthItemEntity.content = perProfile.fertilityNumber.toString()
                        }else{
                            if (perProfile.fertilityStatusId!=-1){
                                healthItemEntity.content=resources.getStringArray(R.array.fertility_status)[perProfile.fertilityStatusId]
                            }
                        }
                    }
                    8 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        healthItemEntity.content = perProfile.occupation
                    }
                    9 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        if (perProfile.illnessNames!=null&&perProfile.illnessNames!="Empty"){
                            healthItemEntity.content = perProfile.illnessNames
                        }
                    }
                    10 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        healthItemEntity.content = perProfile.presentAddress
                    }
                    11 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        var firstContactsLists = perProfile.firstContactsLists
                        if (!(firstContactsLists == null || firstContactsLists.size <= 0)){
                            var name = firstContactsLists[0].name
                            var phone = firstContactsLists[0].phone
                            healthItemEntity.content = name+"\u0009"+phone
                        }
                    }
                    12 -> {
                        healthItemEntity.type=HealthItemEntity.DEFAULT
                    }
                    13 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        healthItemEntity.content=perProfile.motorHabit
                    }
                    14 -> {
                        healthItemEntity.type=HealthItemEntity.FOOTLAYOUT
                        var eatingList = resources.getStringArray(R.array.eating_habit)
                        var eatingHabit:String=""
                        var otherEatingHabit:String=""
                        if (perProfile.eatingHabit!=null){
                            eatingHabit=perProfile.eatingHabit
                        }
                        if(perProfile.otherEatingHabit!=null){
                            otherEatingHabit=perProfile.otherEatingHabit
                        }
                        healthItemEntity.stringList=getList(eatingList,eatingHabit,otherEatingHabit)
                    }
                    15 -> {
                        healthItemEntity.type=HealthItemEntity.FOOTLAYOUT
                        var eatingList = resources.getStringArray(R.array.bad_habit)
                        var badHabits:String=""
                        var otherBadHabits:String=""
                        if (perProfile.badHabits!=null){
                            badHabits=perProfile.badHabits
                        }
                        if (perProfile.otherBadHabits!=null){
                            otherBadHabits=perProfile.otherBadHabits
                        }
                        healthItemEntity.stringList=getList(eatingList,badHabits ,otherBadHabits)
                    }
                    16 -> {
                        healthItemEntity.type=HealthItemEntity.DEFAULT
                    }
                    17 -> {
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        if(perProfile.operationOrNotId!=-1) {
                            healthItemEntity.content = resources.getStringArray(R.array.operation_record)[perProfile.operationOrNotId]
                        }
                    }
                    18 -> {
                        healthItemEntity.type=HealthItemEntity.FOOTLAYOUT
                        var eatingList = resources.getStringArray(R.array.sickness_name)
                        var sicknessName:String=""
                        var otherSicknessName:String=""
                        if (perProfile.sicknessName!=null){
                            sicknessName=perProfile.sicknessName
                        }
                        if(perProfile.otherSicknessName!=null){
                            otherSicknessName=perProfile.otherSicknessName
                        }
                        healthItemEntity.stringList=getList(eatingList,sicknessName,otherSicknessName)
                    }
                    19 -> {
                        healthItemEntity.type=HealthItemEntity.FOOTLAYOUT
                        var eatingList = resources.getStringArray(R.array.drugAllergy_name)
                        var drugAllergy:String=""
                        var otherDrugAllergy:String=""
                        if (perProfile.drugAllergy!=null){
                            drugAllergy=perProfile.drugAllergy
                        }
                        if (perProfile.otherDrugAllergy!=null){
                            otherDrugAllergy=perProfile.otherDrugAllergy
                        }
                        healthItemEntity.stringList=getList(eatingList,drugAllergy,otherDrugAllergy)
                    }
                    20 -> {
                        healthItemEntity.type=HealthItemEntity.FOOTLAYOUT
                        var eatingList = resources.getStringArray(R.array.family_history)
                        var familyHistory:String=""
                        var otherFamilyHistory:String=""
                        if (perProfile.familyHistory!=null){
                            familyHistory=perProfile.familyHistory
                        }
                        if (perProfile.otherFamilyHistory!=null){
                            otherFamilyHistory=perProfile.otherFamilyHistory

                        }
                        healthItemEntity.stringList=getList(eatingList,familyHistory,otherFamilyHistory)
                    }
                    else -> {
                        healthItemEntity.type=HealthItemEntity.DEFAULT
                        healthItemEntity.content = perProfile.name
                    }
                }
                healthList.add(healthItemEntity)
            }
//            //手术记录列表
            var operationLists = perProfile.operationLists
            if (operationLists!=null){
                var array = resources.getStringArray(R.array.operation_list)

                for (operationList in operationLists) {
                    for (i in 0 until 4) {
                        var healthItemEntity: HealthItemEntity = HealthItemEntity()
                        healthItemEntity.type=HealthItemEntity.HEADERLAYOUT
                        when(i){
                            0->{
                                healthItemEntity.title=array[0]
                                healthItemEntity.content=operationList.hospital
                            }
                            1->{
                                healthItemEntity.title=array[1]
                                healthItemEntity.content=operationList.name
                            }
                            2->{
                                healthItemEntity.title=array[2]
                                healthItemEntity.content=operationList.operationTime.substring(0,10)
                            }
                            3->{
                                healthItemEntity.title=array[3]
                                healthItemEntity.content=resources.getStringArray(R.array.operation_result)[operationList.operationResultId]
                            }
                        }
                        healthList.add(healthItemEntity)
                    }

                }
            }
            healthAdapter.notifyDataSetChanged()
        }

    }

    private fun getList(list: Array<String>,  @Nullable string: String, @Nullable otherString: String):List<String>{
        val stringList = ArrayList<String>()
        if(!TextUtils.isEmpty(string)&&string!="Empty"){

            try {
                var splits = string.split(",")
                for (split in splits) {
                    stringList.add(list[split.toInt()])
                }
            } catch (e: Exception) {
            }
        }
        if(otherString!=null&&!TextUtils.isEmpty(otherString)&&otherString!="Empty"){stringList.add(otherString)}
        if (stringList.size==0){stringList.add("未填写")}
        return stringList
    }
}
